class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def path_tree(root):
    """
    找出给定二叉树中所有从根节点到叶节点的路径

    参数：
    - root：二叉树的根节点

    返回值：
    - 所有从根节点到叶节点的路径列表
    """
    def dfs(node, current_path, all_paths):
        """
        深度优先搜索函数，用于遍历二叉树并找出所有路径

        参数：
        - node：当前遍历的节点
        - current_path：当前路径
        - all_paths：所有路径的列表
        """
        if node is None:
            return

        # 将当前节点添加到当前路径中
        current_path.append(str(node.val))

        # 到达叶节点时，将当前路径添加到所有路径中
        if node.left is None and node.right is None:
            all_paths.append("->".join(current_path))
        else:
            # 递归遍历左子树和右子树
            dfs(node.left, current_path, all_paths)
            dfs(node.right, current_path, all_paths)

        # 回溯：从当前路径中移除当前节点
        current_path.pop()

    all_paths = []
    dfs(root, [], all_paths)
    return all_paths


# 示例用法
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(5)

print(path_tree(root))  # 输出: ['1->2->5', '1->3']

root = TreeNode(1)
print(path_tree(root))  # 输出: ['1']


